require(['util', 'validator'], function () {

    $(function(){
        var $safeBehaviorScoreForm = $("#safeBehaviorScoreForm");

        var seleObj = {},
            corpId = "";

        $("#show_data").hide();
        $("#safeBehaviorScoreBnt").hide();

        //左侧树点击事件
        commonFun.leftTreeNodeClickObj['funCheckBehaviorScoreSet'] = function(event, treeId, treeNode) {
            if(treeNode.type === 'CAR' || treeNode.type === 'DRIVER'){
                $.messager.alert("提示","请选择组织机构或线路，进行参数设置。","info");
                return;
            }
            seleObj = treeNode;
            corpId = treeNode.corpId;
            showInterface();
        };

        //根据后台返回的数据，动态生成界面
        var showInterface = function(){
            $.ajaxExt({
                url : "/safeBehaviorScore/searchSafeBehaviorScoreInfo.htm",
                data : {"corpId":corpId, "organId":seleObj.id}
            }).done(function(data){
                var currData = data;
                //清空界面，后面再根据数据渲染界面
                $("#u_tip").remove();
                $("#sele_msg").empty();
                $('#sbScoreWV').empty();
                $('#sbDedPoint').empty();
                $('#sbScoreAC').empty();
                //显示界面
                $("#show_data").show();
                $("#safeBehaviorScoreBnt").show();
                //根据数据渲染界面
                var seleObjId = seleObj.id,
                    currObjId = currData[0].organLineId;
                $("#sele_msg").append("<h1 class='line-title'>【"+seleObj.name+"】的驾驶安全行为评分标准信息</h1>");
                $("#sele_msg").append("<input type='hidden' name='seleId' value='"+seleObjId+"'/>"
                +"<input type='hidden' name='corpId' value='"+corpId+"'/>"
                +"<input type='hidden' name='currId' value='"+currObjId+"'/>");
                $.each(currData,function(n,entity){
                    $("#sbScoreWV").append("<input type='hidden' name='chkIds' value='"+entity.chkId+"'/>");
                    $('#sbScoreWV').append("<li>"
                    +"<label class='label-text'>"+entity.chkName+"评分权重：</label>"
                    +"<input type='text' name='sbScoreWVs' class='input-text length-small' data-validator='{required:true,number:true}' value='"+entity.weightValue+"'/> %"
                    +"</li>");
                    $('#sbDedPoint').append("<li>"
                    +"<label class='label-text'>发生"+entity.chkName+"扣分：</label>"
                    +"<input type='text' name='sbDedPoints' class='input-text length-small' data-validator='{required:true,number:true}' value='"+entity.deductValue+"'/>"
                    +"</li>");
                    $('#sbScoreAC').append("<li>"
                    +"<label class='label-text'>允许"+entity.chkName+"次数：</label>"
                    +"<input type='text' name='sbScoreACs' class='input-text length-small' data-validator='{required:true,number:true}' value='"+entity.allowCount+"'/>"
                    +"</li>");
                });
            });
        };

        $("#safeBehaviorScore_update").on("click", function(){
            if($.validator($('#safeBehaviorScoreForm'))){
                $.messager.confirm("是否修改","确定修改该机构的驾驶安全行为评分标准信息？",function(r){
                    if(r){
                        var formData = $safeBehaviorScoreForm.serialize();
                        $.ajaxExt({
                            url : "/safeBehaviorScore/uptSafeBehaviorScoreInfo.htm",
                            data : formData
                        }).done(function(data){

                        });
                    }
                });
            }
        });

        $("#safeBehaviorScore_reset").on("click", function(){
            showInterface();
        });
        
        //页面打开自动查询
        var nodes = commonFun.getLeftTreeSelectedNodes();
		if(nodes.length<1||nodes[0].type==="LINE"){
			seleObj = nodes[0];
            corpId = nodes[0].corpId;
            showInterface();
		}
    });
})